<?php
//无限分类类库
require_once "tools/function/FunctionUtils.php";
class SortClass{

	var $data = array();
	var $key = array();
	var $remark = array();
	var $child = array(-1=>array());
	var $layer = array(-1=>-1);
	var $parent = array();
	var $db;
	var $table;
	var $datatable="t_reported_data";
    var $showhtml;
	function SortClass($db, $table){
		#if(!FunctionUtils::deny()){die(@FunctionUtils::emptyJSON("IP受限"));}
		$this->setNode(0, -1, '顶极节点','','');
		$this->db = $db;
		$this->table = $table;
		$node = array();
		$results = $db->query_all('select * from '.$this->table.' order by sys_pid');
		
		foreach($results as $node){
			$this->setNode($node['sys_id'],$node['sys_pid'],$node['sys_name'],$node['sys_key'],$node['sys_remark']);
		}
	}
	function setNode ($id, $parent, $value,$key,$remark){
		$parent = $parent?$parent:0;
		$this->data[$id] = $value;
		$this->key[$id] = $key;
		$this->remark[$id] = $remark;
		$this->child[$id] = array();
		$this->child[$parent][] = $id;
		$this->parent[$id] = $parent;
		$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
	}
	function getList (&$tree, $root= 0){
		foreach ($this->child[$root] as $key=>$id){
			$tree[] = $id;
			if ($this->child[$id]) $this->getList($tree, $id);
		}
	}
	function getValue ($id){return $this->data[$id];}
	function getKey ($id){return $this->key[$id];}
	function getRemark ($id){return $this->remark[$id];}
	function getLayer ($id, $space = false){
		return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
	}
	function getParent ($id){return $this->parent[$id];}
	function getParents ($id){
		while ($this->parent[$id] != -1){
			$id = $parent[$this->layer[$id]] = $this->parent[$id];
		}
		ksort($parent);
		reset($parent);
		return $parent;
	}
	function getChild ($id){return $this->child[$id];}
	function getChilds ($id = 0){
		$child = array($id);
		$this->getList($child, $id);
		return $child;
	}
	function checkkeyisexsit($key){
		$db=$this->db;
		$sql="select sys_id from t_reported_sys where sys_key='$key'";
		if(!$result=$db->query($sql))die(@FunctionUtils::emptyJSON("查询KEY是否存在发生错误"));
		$num=$db->num_rows($result);
		if($num>=1)die(@FunctionUtils::emptyJSON($key."已经存在,请重新定义"));
	}
	function addNode($name,$remark,$pid){
		$db=$this->db;
		//$this->checkkeyisexsit($key);
		
		#$this->create_key_datatable_ifnotexsit($key);
		if(!$db->query("insert into $this->table (`sys_pid`,`sys_name`,sys_remark) values ('$pid','$name','$remark')")){
			die(@FunctionUtils::emptyJSON("添加失败"));
		}
		else die(@FunctionUtils::emptyJSON("添加成功",0));
	}
	function modNode($cid, $newName,$newkey,$newremark){
		$db=$this->db;
		try {
			if($db->query("update $this->table set `sys_name`='$newName',`sys_remark`='$newremark',`sys_key`='$newkey' where `sys_id` = $cid")){
				die(@FunctionUtils::emptyJSON("修改成功",0));
			}
		} catch (Exception $e) {
			die(@FunctionUtils::emptyJSON("修改失败"));
		}
		
	}
	function delNode($cid,$key){
		$db=$this->db;
		$allChilds = $this->getChilds($cid);
		$sql ='';
		if(empty($allChilds)){
			$sql = "delete from $this->table where `sys_id` = $cid";
		}else{
			$sql = 'delete from '.$this->table.' where `sys_id` in ('.implode(',',$allChilds).','.$cid.')';
		}
		try {
			if($db->query($sql))die(@FunctionUtils::emptyJSON("删除成功",0));
		} catch (Exception $e) {
			die(@FunctionUtils::emptyJSON("删除失败"));
		}
		
	}
	function moveNode($cid, $topid){
		$db=$this->db;
		try {
			if($db->query("update $this->table set `sys_pid`=$topid where `sys_id` = $cid"))die(@FunctionUtils::emptyJSON("移动成功",0));
		} catch (Exception $e) {
			die(@FunctionUtils::emptyJSON("移动失败"));
		}
		
	}
    ///***以下为现实列表逻辑函数////
    function productscate($category){
        $this->showhtml.="<ul>";
        foreach ($category as $id){
            $childs=$this->getChild($id);
            $parentid=$this->getParent($id);
            $havechild=empty($childs)?false:true;
            if($havechild){
                $this->showhtml.= '<li><a class="cur" href="javascript:;" onclick="list_imagedb('.$id.',\'\',\'\')" title="">'.$this->getValue($id).'</a>';
                $this->productscate($childs);
            }else{
                $this->showhtml.= '<li><a href="javascript:;" onclick="list_imagedb('.$id.',\'\',\'\')" title="">'.$this->getValue($id).'</a></li>';
            }
        }
        $this->showhtml.="</ul>";
    }
    function showtreelist($id){//需要显示的id
        $this->productscate($this->getChild($id));
    }
    function makeTreeSelect($id,$formName){;
        $select = '<select name="'.$formName.'" id="'.$formName.'"><option value="">图片分类</option>';
        $category=$this->getChild($id);
        foreach ($category as $id){
            $select.='<option value="'.$id.'">'.$this->getLayer($id, '-').$this->getValue($id)."</option>";
        }
        return $select.'</select>';
    }
}